home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TOP_LEVE.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  9.1 KB  |  304 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.output.drawable;
  4. import sub_arctic.output.style_manager;
  5.  
  6. import java.awt.Component;
  7. import java.awt.Point;
  8. import java.awt.Dimension;
  9. import java.awt.Rectangle;
  10. import java.awt.Image;
  11.  
  12. /** 
  13.  * This class is the top interactor of a sub_arctic interface.
  14.  * top_level interactors are hosted in AWT components like 
  15.  * interactor_frame, interactor_applet, and interactor_canvas.
  16.  * top_levels should not have constraints placed on their width, height
  17.  * or position, at least not  once they have been hosted into an
  18.  * AWT component. The width and height of a top_level are controlled
  19.  * by its containing component.
  20.  *
  21.  * @author Scott Hudson
  22.  */
  23. public class top_level extends base_parent_interactor {
  24.  
  25.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  26.  
  27.   /*
  28.    * Pointer to our hosting component.
  29.    */
  30.   protected Component _awt_parent = null;
  31.  
  32.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  33.  
  34.   /**
  35.    * Set the component that this this interactor is hosted in.
  36.    * @param Component par the hosting component.
  37.    */
  38.   public void set_awt_parent(Component par) 
  39. {
  40.       if (_awt_parent != null && par == null) 
  41.     {
  42.           _awt_parent = par;
  43.       manager.remove_top_level(this);
  44.     }
  45.       else if (_awt_parent == null && par != null);
  46.     {
  47.           _awt_parent = par;
  48.       manager.add_top_level(this);
  49.       damage_self();
  50.     }
  51.     }
  52.  
  53.    //had:
  54.    //* @exception general PROPAGATED
  55.  
  56.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  57.  
  58.   /**
  59.    * Return the AWT component which hosts this interactor.
  60.    * @return Component the AWT component hosting this interactor
  61.    */
  62.   public Component awt_parent() {return _awt_parent;}
  63.  
  64.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  65.  
  66.   /**
  67.    * Our backing store (offscreen copy of the image of the interface)
  68.    */
  69.   protected Image _offscreen_image = null;
  70.  
  71.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  72.  
  73.   /** 
  74.    * The drawing context for putting graphics on the backing store
  75.    */
  76.   protected drawable _offscreen_graphics = null;
  77.  
  78.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  79.  
  80.   /**
  81.    * This is the size of the backing store. 
  82.    */
  83.   protected Dimension _offscreen_size = null;
  84.  
  85.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  86.  
  87.   /**
  88.    * Create the offscreen storage used for the backing store. 
  89.    * @param drawable screen_d the drawable corresponding to the screen
  90.    */
  91.   protected void setup_offscreen(drawable screen_d) 
  92. {
  93.       /* get parent and parent's current size */
  94.       Component parent = awt_parent();
  95.       Dimension sz = parent.size();
  96.  
  97.       /* catch zero size since AWT doesn't like that */
  98.       if (sz.width <= 0)  sz.width = 1;
  99.       if (sz.height <= 0) sz.height = 1;
  100.  
  101.       /* do we need to allocate/reallocate */
  102.       if (_offscreen_image == null || sz.width != _offscreen_size.width || 
  103.                       sz.height != _offscreen_size.height)
  104.     {
  105.       /* remember the size */
  106.       _offscreen_size  = sz;
  107.  
  108.       /* create an image and a graphics context that refers to it */
  109.       _offscreen_image = parent.createImage(sz.width, sz.height);
  110.       _offscreen_graphics = new drawable(_offscreen_image.getGraphics());
  111.  
  112.       /* fill in state to match the one for the screen */
  113.       _offscreen_graphics.setColor(screen_d.getColor());
  114.       _offscreen_graphics.setFont(screen_d.getFont());
  115.  
  116.       /* now need to redraw everything */
  117.       damage_self();
  118.     }
  119.     }
  120.  
  121.    //had:
  122.    //* @exception general PROPAGATED
  123.  
  124.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  125.  
  126.   /**
  127.    * Construct a top_level object with know x,y, width and height.
  128.    * @param int x_v the x coordinate of this interactor (should be zero 
  129.    *                unless you want this interactor to not cover all of its 
  130.    *                hosting AWT parent).
  131.    * @param int y_v the y coordinate of this interactor (should be zero 
  132.    *                unless you want this interactor to not cover all of its 
  133.    *                hosting AWT parent).
  134.    * @param int w_v the width of this top_level (this is also tied to the size 
  135.    *                of the AWT component).
  136.    * @param int h_v the height of this top_level (this is also tied to the size
  137.    *                of the AWT component).
  138.    */
  139.   public top_level(int x_v, int y_v, int w_v, int h_v) 
  140. {
  141.       super(x_v,y_v,w_v,h_v);
  142.  
  143.       /* whole region is damaged to start with */
  144.       _damage_area = new Rectangle(0,0, w(),h());
  145.     }
  146.  
  147.    //had:
  148.    //* @exception general PROPAGATED
  149.  
  150.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  151.  
  152.   /**
  153.    * Construct a top level and give it default values for x,y, width and
  154.    * height. We assume that the system will fill in these values later.
  155.    */
  156.   public top_level() 
  157. {
  158.       this(0,0,0,0);
  159.     }
  160.  
  161.    //had:
  162.    //* @exception general PROPAGATED
  163.  
  164.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  165.  
  166.   /**
  167.    * Draw the image of this interactor (and its children) on the supplied
  168.    * drawable.  This draws first on an off-screen image then throws the 
  169.    * whole thing up on the screen in one drawing operation.
  170.    *
  171.    * @param drawable parent_d the drawable to render the image on
  172.    */
  173.   public void draw_self(drawable parent_d) 
  174. {
  175.       drawable draw_graphics;
  176.  
  177.       /* only recreate image if we have been damaged */
  178.       if (flag_is_set(DAMAGED)) 
  179.     {
  180.           /* make sure we have backing store and its sized right */
  181.           setup_offscreen(parent_d);
  182.  
  183.           /* make a temp copy of the graphics context */
  184.           draw_graphics = _offscreen_graphics.copy();
  185.  
  186.       /*
  187.        * This is to enforce the color scheme the user is 
  188.        * using for the background.
  189.        */
  190.       draw_graphics.setColor(style_manager.
  191.                  default_color_scheme().base());
  192.  
  193.           /* set clip to damaged area and clear it to the current background */
  194.           if (_damage_area != null)
  195.         {
  196.           draw_graphics.clipRect(_damage_area.x,     _damage_area.y, 
  197.                          _damage_area.width, _damage_area.height);
  198.           draw_graphics.fillRect(_damage_area.x,     _damage_area.y, 
  199.                           _damage_area.width, _damage_area.height);
  200.         }
  201.       else
  202.         {
  203.               /* clip already set to whole image, so just clear */
  204.               draw_graphics.clearRect(0,0,
  205.                 _offscreen_size.width,_offscreen_size.height);
  206.         }
  207.       
  208.       /* 
  209.        * set the foreground color to the user's choice.
  210.        */
  211.       draw_graphics.setColor(style_manager.
  212.                  default_color_scheme().foreground());
  213.  
  214.           /* do the real drawing on the offscreen image */
  215.           super.draw_self(draw_graphics);
  216.         }
  217.  
  218.       /* put up the image */
  219.       parent_d.drawImage(_offscreen_image, 0,0, manager.an_observer());
  220.     }
  221.  
  222.    //had:
  223.    //* @exception general PROPAGATED
  224.  
  225.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  226.  
  227.   /** 
  228.    * Configure the subtree after it has been damaged, but before it is redrawn.
  229.    * Here we are careful not to start an actual  configure pass if we have 
  230.    * not been damaged since we get redraw requests at the top level due to 
  231.    * "outside" damage such as window exposures.
  232.    */
  233.   public void configure()
  234.     {
  235.       /* do a normal configure if and only if we have damage */
  236.       if (flag_is_set(DAMAGED)) super.configure();
  237.     }
  238.  
  239.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  240.  
  241.   /**
  242.    * Clear the dirty bit for this object (object is not damaged)
  243.    */
  244.   protected void damage_fixed()
  245.     {
  246.       clear_flag_bit(DAMAGED);
  247.       _damage_area = null;
  248.     }
  249.  
  250.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  251.  
  252.   /** This holds the area of damage to this interactors display */
  253.   protected Rectangle _damage_area = null;
  254.  
  255.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  256.  
  257.   /**
  258.    * Set the dirty bits and accumulate a damage rectangle of the
  259.    * size at the given point.
  260.    * 
  261.    * @param Point     top_left the upper left corner of the damaged area.
  262.    * @param Dimension sz       the size of the damaged area.
  263.    */
  264.   public void damage_self(Point top_left, Dimension sz) 
  265. {
  266.       /* note damage */
  267.       set_flag_bit(DAMAGED);
  268.  
  269.       /* accumulate damaged area */
  270.       if (_damage_area != null)
  271.     {
  272.       _damage_area = _damage_area.union(new Rectangle(top_left, sz));
  273.     }
  274.       else
  275.     {
  276.       _damage_area = new Rectangle(top_left, sz);
  277.     }
  278.  
  279.       /* inform the overall system that we need to be redrawn */
  280.       manager.report_damage(this);
  281.     }
  282.  
  283.    //had:
  284.    //* @exception general PROPAGATED
  285.  
  286.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  287. };
  288. /*=========================== COPYRIGHT NOTICE ===========================
  289.  
  290. This file is part of the subArctic user interface toolkit.
  291.  
  292. Copyright (c) 1996 Scott Hudson and Ian Smith
  293. All rights reserved.
  294.  
  295. The subArctic system is freely available for most uses under the terms
  296. and conditions described in 
  297.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  298. and appearing in full in the lib/interactor.java source file.
  299.  
  300. The current release and additional information about this software can be 
  301. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  302.  
  303. ========================================================================*/
  304.